package cn.edu.scujcc;

import java.util.List;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class MusicService {
	@Autowired
	private MusicRepository repo;
	
	public List<Music> getAllMusic(){
		List<Music>result = null;
		result = repo.findAll();
		return result;
	}
	public Music getMusic(String id) {
		Music result = null;
		Optional<Music> op = repo.findById(id);
		if (op.isPresent()) {
			result = op.get();
			}
		return result;
		}
	
	public Music createComment(String id) {
		Music result = null;
		Optional<Music> op = repo.findById(id);
		if (op.isPresent()) {
			result = op.get();
			}
		return result;
		}
	public Music createMusic(Music m) {
		Music result = null;
		result = repo.save(m);
		return result;
	}
	public Music updateMusic(Music m) {
		Music result = null;
		result = repo.findById(m.getId()).get();
		if(result !=null) {
		if(m.getName()!=null){
			result.setName(m.getName());
		}
		if(m.getSeller()!=null){
			result.setSeller(m.getSeller());
		}
		if(m.getComment()!=null){
			result.setComment(m.getComment());
		}
		if(m.getCover()!=null){
			result.setCover(m.getCover());
		}
		if(m.getTime()!=null){
			result.setTime(m.getTime());
		}
		if(m.getIntroduction()!=null){
			result.setIntroduction(m.getIntroduction());
		}
		if(m.getHeat()!=null){
			result.setHeat(m.getHeat());
		}
		result = repo.save(result);
		}
		return result;
	}
}